using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;
using UnityEngine.UI;
public class StartPanel : Panel
{

    #region 字段
    [SerializeField] private Button btn_Read;
    [SerializeField] private Button btn_Quit;
    [SerializeField] private Button btn_NewGame;
    [SerializeField] private Text text_content;
    [SerializeField] private Button btn_Setting;

    private void Reset()
    {
        btn_Read = transform.Find("btn_Read").GetComponent<Button>();
        btn_Quit = transform.Find("btn_Quit").GetComponent<Button>();
        btn_NewGame = transform.Find("btn_NewGame").GetComponent<Button>();
        text_content = transform.Find("btn_NewGame/text_content").GetComponent<Text>();
        btn_Setting = transform.Find("btn_Setting").GetComponent<Button>();
    }
    private GameController gameController => Module.LoadController<GameController>();
    private GameSaveController gameSaveController => Module.LoadController<GameSaveController>();

    #endregion

    #region 方法
    public void UpdateStartPanel()
    {
        if (gameSaveController.GetExists())
        {
            text_content.text = "继续游戏";
            btn_Read.interactable = true;
        }
        else
        {
            text_content.text = "新游戏";
            btn_Read.interactable = false;
        }
    }
    #endregion
    #region UI事件监听
    public void OnBtnSettingClick()
    {
        Module.LoadPanel<SettingPanel>();
    }
    public void OnBtnQuitClick()
    {
        TipPanel tipPanel = Module.LoadPanel<TipPanel>();
        tipPanel.ShowTips("提示",
         "小斑鸠，你真的要退出游戏吗？",
         () =>
         {
             Application.Quit();
#if UNITY_EDITOR
             UnityEditor.EditorApplication.isPlaying = false;
#endif
         },
         () => tipPanel.Close(),
  true,
  "是的喵",
  "并非退出");

    }
    public void OnBtnReadClick()
    {
        Module.LoadPanel<GameSavePanel>(UIType.UI,null,this);
    }
    public void OnBtnNewGameClick()
    {
        if (gameSaveController.GetExists())
        {
            PlayerModel playerModel = gameSaveController.GetPlayerModel(gameSaveController.GetArchiveCount() - 1);  
            gameController.SetArchiveIndex(playerModel.Id);
            gameController.SetGameState(GameState.Game_Ready);
        }
        else
        {
            gameSaveController.DeleteGameSave(0);
            gameSaveController.CreateGameSave(0);
            gameSaveController.CoverGameSave(1);
            gameController.SetArchiveIndex(1);
            gameController.SetGameState(GameState.Game_Ready);
        }

    }
    #endregion
    #region 重写方法
    public override void OnLoaded(params object[] param)
    {
        base.OnLoaded(param);
        
        GameSaveController gameSaveController = Module.LoadController<GameSaveController>();
        UpdateStartPanel();
    }
    #endregion
}


